﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sigil.Impl
{
	internal static class Extensions
	{
		private static List<TypeOnStack> _PeekWildcard = new List<TypeOnStack>(new[] { TypeOnStack.Get<WildcardType>() });

		internal static List<TypeOnStack>[] Peek(this Stack<List<TypeOnStack>> stack, Boolean baseless, Int32 n)
		{
			if (stack.Count < n && !baseless) { return null; }

			var ret = new List<TypeOnStack>[n];

			Int32 i;
			for (i = 0; i < n && i < stack.Count; i++)
			{
				ret[i] = stack.ElementAt(i) as List<TypeOnStack>;
			}

			while (i < n)
			{
				ret[i] = _PeekWildcard;
				i++;
			}

			return ret;
		}

		internal static void Each<T>(this IEnumerable<T> e, Action<T> a)
		{
			var arr = e as T[];
			if (arr != null)
			{
				for (var i = 0; i < arr.Length; i++)
				{
					a(arr[i]);
				}

				return;
			}
			foreach (var x in e)
			{
				a(x);
			}
		}
	}
}
